//
//  LGHotPicker.m
//  SportRing
//
//  Created by mykoma on 14-4-14.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "LGHotPicker.h"

#define EACH_SCROLL_VIEW_WIDTH (60)

@interface LGHotPicker()
<
UIScrollViewDelegate
>
{
    UIScrollView * _scrollView;
    NSMutableDictionary * _whiteLabels;
}

@property (nonatomic, strong) NSMutableArray * values;

@end

@implementation LGHotPicker

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.values = [NSMutableArray array];
    }
    return self;
}

-(void)setupViews
{
    _whiteLabels = [NSMutableDictionary dictionary];
}

-(void)willMoveToSuperview:(UIView *)newSuperview
{
    [self setupScrollViews];
}

-(void)setupScrollViews
{
    self.backgroundColor = [UIColor whiteColor];
    
    NSUInteger numberOfRow = [self.dataSource numberOfRow];
    
    // 在最左侧添加一个View, 美化界面
    UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(self.offsetX - EACH_SCROLL_VIEW_WIDTH / 3,
                                                                 self.offsetY + EACH_SCROLL_VIEW_WIDTH,
                                                                 EACH_SCROLL_VIEW_WIDTH,
                                                                 EACH_SCROLL_VIEW_WIDTH)];
    leftView.layer.cornerRadius = EACH_SCROLL_VIEW_WIDTH / 3;
    leftView.backgroundColor = [UIColor red:30 green:177 blue:74 alpha:1.0];
    [self addSubview:leftView];

    // 在最右侧添加一个View, 美化界面
    UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(self.offsetX + (numberOfRow - 2.0 / 3) * EACH_SCROLL_VIEW_WIDTH,
                                                                  self.offsetY + EACH_SCROLL_VIEW_WIDTH,
                                                                  EACH_SCROLL_VIEW_WIDTH,
                                                                  EACH_SCROLL_VIEW_WIDTH)];
    rightView.layer.cornerRadius = EACH_SCROLL_VIEW_WIDTH / 3;
    rightView.backgroundColor = [UIColor red:30 green:177 blue:74 alpha:1.0];
    [self addSubview:rightView];
    
    for (int row = 0; row < numberOfRow; row++)
    {
        [self setupViewAtRow:row];
    }
    
    // 添加确认按钮, 关闭程序
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.layer.cornerRadius = 5.0f;
    button.frame = CGRectMake(20, 400, 280, 44);
    button.backgroundColor = [UIColor red:250 green:105 blue:10 alpha:1];
    [button setTitle:@"确    认" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(okButtonTouched) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
}

-(void)setupViewAtRow:(NSUInteger)row
{
    NSArray * contents = [self.dataSource charsAtRow:row];
    self.values[row] = contents[0];
    
    // 创建UIScrollView
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.offsetX + EACH_SCROLL_VIEW_WIDTH * row,
                                                                               self.offsetY,
                                                                               EACH_SCROLL_VIEW_WIDTH,
                                                                               EACH_SCROLL_VIEW_WIDTH * 3)];
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollEnabled = YES;
    scrollView.delegate = self;
    scrollView.tag = row;
    __block CGFloat offsetY = 0;
    
    // 添加一个背景颜色
    UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(scrollView.frame),
                                                               CGRectGetMinY(scrollView.frame) + EACH_SCROLL_VIEW_WIDTH,
                                                               EACH_SCROLL_VIEW_WIDTH,
                                                               EACH_SCROLL_VIEW_WIDTH)];
    bgView.backgroundColor = [UIColor red:30 green:177 blue:74 alpha:1.0];
    [self addSubview:bgView];
    
    // 在开头的地方添加一个空位
    UILabel * topSpace = [[UILabel alloc] initWithFrame:CGRectMake(0, EACH_SCROLL_VIEW_WIDTH, EACH_SCROLL_VIEW_WIDTH, EACH_SCROLL_VIEW_WIDTH)];
    offsetY = EACH_SCROLL_VIEW_WIDTH;
    [scrollView addSubview:topSpace];
    
    // 循环将内容添加到scrollView
    [contents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UILabel * contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, offsetY, EACH_SCROLL_VIEW_WIDTH, EACH_SCROLL_VIEW_WIDTH)];
        contentLabel.text = contents[idx];
        contentLabel.textAlignment = NSTextAlignmentCenter;
        contentLabel.textColor = [UIColor lightGrayColor];
        contentLabel.font = [UIFont systemFontOfSize:30];
        
        if (idx == 0)
        {
            _whiteLabels[@(scrollView.tag)] = contentLabel;
            
            contentLabel.textColor = [UIColor whiteColor];
        }
        
        [scrollView addSubview:contentLabel];
        offsetY += EACH_SCROLL_VIEW_WIDTH;
    }];
    
    // 在结尾的地方添加一个空位
    UILabel * bottomSpace = [[UILabel alloc] initWithFrame:CGRectMake(0, offsetY, EACH_SCROLL_VIEW_WIDTH, EACH_SCROLL_VIEW_WIDTH)];
    [scrollView addSubview:bottomSpace];
    
    // 设置滚动区域
    scrollView.contentSize = CGSizeMake(0, EACH_SCROLL_VIEW_WIDTH * (contents.count + 2));
    
    [self addSubview:scrollView];
}

#pragma mark - Private Methods

-(void)okButtonTouched
{
    if([self.delegate respondsToSelector:@selector(hotPickerWillDisappear:)])
    {
        [self.delegate hotPickerWillDisappear:self];
    }
    
    [self removeFromSuperview];
}

-(CGFloat)offsetX
{
    return (320 - [self.dataSource numberOfRow] * EACH_SCROLL_VIEW_WIDTH) / 2;
}

-(CGFloat)offsetY
{
    return (480 - EACH_SCROLL_VIEW_WIDTH * 3) / 2;
}

-(NSUInteger)getCurrentOffsetIndex:(UIScrollView *)scrollView
{
    return (scrollView.contentOffset.y / EACH_SCROLL_VIEW_WIDTH + 0.5);
}

-(void)scrollToOffsetIndex:(UIScrollView *)scrollView
{
    NSUInteger offsetIndex = [self getCurrentOffsetIndex:scrollView];
    [scrollView setContentOffset: CGPointMake(scrollView.contentOffset.x, offsetIndex * EACH_SCROLL_VIEW_WIDTH)  animated:YES];
    
    // 设置新的label
    UILabel * label = scrollView.subviews[offsetIndex + 1];
    label.textColor = [UIColor whiteColor];
    _whiteLabels[@(scrollView.tag)] = label;
    
    self.values[scrollView.tag] = [self.dataSource charsAtRow:scrollView.tag][offsetIndex];
    
    if ([self.delegate respondsToSelector:@selector(hotPicker:selectIndex:atRow:)])
    {
        [self.delegate hotPicker:self selectIndex:offsetIndex atRow:scrollView.tag];
    }
}

#pragma mark - UIScrollViewDelegate

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 还原已经改变颜色的label
    UILabel * label = _whiteLabels[@(scrollView.tag)];
    label.textColor = [UIColor lightGrayColor];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self scrollToOffsetIndex:scrollView];
}

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self scrollToOffsetIndex:scrollView];
}

@end
